<?
/* UTF8 marker äöü */
class ccdsTrash
	extends ccdsBackendPlugin
{
	private $name = "ccdsTrash";

	function __construct()
	{
		parent::__construct();

		$this->xajax = ccdsXajax::getInstance();

		$this->xajax->register( XAJAX_CCDS_FUNCTION, "TrashOpen", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "TrashListing", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "TrashMenu", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "TrashRestore", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "TrashDelete", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "_TrashDelete", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "TrashDeleteAll", $this->name );
	}

	// -------------------------------------------------------------------------------------------------------------------

	function TrashOpen( $aFormValues )
	{

		$objResponse = new ccdsResponse();
		
		$this->smarty->assign("project_id",$aFormValues['project_id']);
		$sHtml = $this->smarty->fetch( $this->config->get( 'TPL_DIR' )."trash/body.tpl" );

		$icon_src = $this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/bin.png";

		$objResponse->loadcommands( $this->tools->addTabManager( 'topTab', "TrashTab_".$aFormValues['project_id'], "<img src=\"".$icon_src."\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Mülleimer</span>" ) );

		$objResponse->clsTabManager->addPanel( "TabManagerContent_TrashTab_".$aFormValues['project_id'], array
		(
			"id" => "TrashContainer_".$aFormValues['project_id'],
			"title" => "<img src=\"".$this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/bin_closed.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Mülleimer</span>",
			"content" => "<div id='TrashContent_".$aFormValues['project_id']."'></div>",
			"closeable" => false
		));
		
		$objResponse->assign( "TrashContent_".$aFormValues['project_id'], "innerHTML", $sHtml );
		$objResponse->loadcommands( $this->TrashListing( array ("project_id" => $aFormValues['project_id'])) );

		return $objResponse;
	}



	// -------------------------------------------------------------------------------------------------------------------

	function TrashListing( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		$db_trash = array ();

		$trash_versions_where = "";
		$modifier = "";
		$query = "
	           SELECT *
	           FROM obj_versions
	           LEFT OUTER JOIN obj_index ON obj_vers_key_obj_id = obj_id
	           WHERE obj_id IS NULL
	           ";
		$res = $this->ccdSQL->query( $query );
		$db_trash['versions'] = $this->ccdSQL->num_rows( $res );
		$trash_versions = array ();

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$trash_versions[] = array
			(
				"obj_vers_id" => $row['obj_vers_id'],
				"linked_table" => ""
			);

			$trash_versions_where .= $modifier.$row['obj_vers_id'];
			$modifier = " , ";
		}

		if ( count( $trash_versions ) > 0 )
		{
			$query = "SELECT * FROM classes";
			$res = $this->ccdSQL->query( $query );

			while ( $class = $this->ccdSQL->fetch_assoc( $res ) )
			{

				$object = new ccdsObjectHandler( $class['class_id']);
				$this->smarty->assign( "obj_vers_id", $obj_vers_id );

				if ( $object->has_tables )
				{
					$where = $object->__getIDfield( $object->config['tables'] )." IN (".$trash_versions_where.")";
					$rows = $object->getEntryList( $_SESSION['CMS']['user_lang'], $class['class_id'], $where );
					$query = "SELECT * FROM ".$object->config['tables']['name']."";
				}
			}
		}

		$deleted_objects = array ();

		$query = "SELECT obj_id FROM obj_index
	           WHERE 1
	           AND obj_deleted='1'
	           AND obj_key_project_id='".$aFormValues['project_id']."'
	
	           ";
		$res = $this->ccdSQL->query( $query );

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{

			$row = $this->tools->getObjectDetails( $row['obj_id'], $_SESSION['CMS']['user_lang_id'], false );

			$query = "SELECT * FROM obj_index WHERE obj_id='".$row['obj_parent_obj_id']."'";
			$res2 = $this->ccdSQL->query( $query );
			$parent = $this->ccdSQL->fetch_assoc( $res2 );

			if ( $parent['obj_deleted'] != 1 )
			{
				$row['count'] = $this->tools->CountObjects( $row['obj_id'], 1 );
				$row['path'] = $this->tools->getRecursivePath( $row['obj_id'] );
				$deleted_objects[] = $row;
			} 
		}

		$this->smarty->assign( "deleted_objects", $deleted_objects );
		$this->smarty->assign( "project_id", $aFormValues['project_id'] );

		$sHtml = $this->smarty->fetch( $this->config->get( 'TPL_DIR' )."trash/listing.tpl" );

		$objResponse->assign( "divTrashContent_".$aFormValues['project_id'], "innerHTML", $sHtml );

		return $objResponse;
	}

	// -------------------------------------------------------------------------------------------------------------------

	function TrashMenu( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		$query = "SELECT COUNT(obj_id) as anzahl FROM obj_index WHERE obj_deleted=1";
		$res = $this->ccdSQL->query( $query );
		$row = $this->ccdSQL->fetch_assoc( $res );

		if ( $row['anzahl'] > 0 )
		{
			$icon = $this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/bin.png";
		}
		else
		{
			$icon = $this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/bin_closed.png";
		}

		$objResponse->script( "$('#navi_trash_".$aFormValues['project_id']." .icon').css('background-image','".$icon."');" );
		$objResponse->script( "$('#navi_trash_".$aFormValues['project_id']." .text').text('Papierkorb (".$row['anzahl'].")');" );

		return $objResponse;
	}


	// -------------------------------------------------------------------------------------------------------------------

	function TrashRestore( $aFormValues )
	{

		$objResponse = new ccdsResponse();
		$query = "SELECT * FROM obj_index WHERE obj_id='".$aFormValues['obj_id']."'";
		$res = $this->ccdSQL->query( $query );
		$obj = $this->ccdSQL->fetch_assoc( $res );

		$this->tools->MarkObjectDeleted( $aFormValues['obj_id'], true, 0 );

		$objResponse->loadcommands( $this->TrashListing( array ("project_id" => $obj['obj_key_project_id'])) );
		$objResponse->loadcommands( $this->TrashReload( array ("project_id" => $obj['obj_key_project_id'])) );

		$objResponse->loadClassCommand( "ccdsDefaultPlugin", "updateNaviEntry", array 
			(
				"project_id" => $obj['obj_key_project_id'],
				"obj_parent_obj_id" => $obj['obj_parent_obj_id']
			),true);

		return $objResponse;
	}
	// -------------------------------------------------------------------------------------------------------------------

	function TrashDelete( $aFormValues )
	{

		$objResponse = new ccdsResponse();
		$count = $this->tools->CountObjects( $aFormValues['obj_id'], 1 );

		if ( $count > 0 )
		{

			$objResponse->script( "$('<div />').xWindow('confirm',{title:'Papierkorb',content:'Das Objekt enhält ".$count." weitere Objekte!<br />Wirklich löschen?'},function() {xajax.request({ccdsfun:'_TrashDelete',ccdsclass:'ccdsTrash'},{parameters:[{obj_id:'".$aFormValues['obj_id']."'}]});return true;});");
		}
		else
		{
			$objResponse->loadcommands( $this->_TrashDelete( array ("obj_id" => $aFormValues['obj_id'])) );
		}

		return $objResponse;
	}
	// -------------------------------------------------------------------------------------------------------------------

	function _TrashDelete( $aFormValues )
	{

		$objResponse = new ccdsResponse();

		$query = "SELECT * FROM obj_index WHERE obj_id='".$aFormValues['obj_id']."'";
		$res = $this->ccdSQL->query( $query );
		$obj = $this->ccdSQL->fetch_assoc( $res );

		$this->tools->ObjectDelete( $aFormValues['obj_id'] );

		$objResponse->assign( "trashobj".$aFormValues['obj_id'], "style.display", "none" );
		$objResponse->loadcommands( $this->TrashReload( array ("project_id" => $obj['obj_key_project_id'])) );

		return $objResponse;
	}

	// -------------------------------------------------------------------------------------------------------------------

	function TrashReload( $aFormValues )
	{

		$objResponse = new ccdsResponse();
		$query = "SELECT COUNT(*) as anzahl FROM obj_index WHERE obj_deleted=1 AND obj_key_project_id='".$aFormValues['project_id']."'";
		$res = $this->ccdSQL->query( $query );
		$row = $this->ccdSQL->fetch_assoc( $res );

		if ( $row['anzahl'] > 0 )
		{
			$icon = $this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/bin.png";
		}
		else
		{
			$icon = $this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/bin_closed.png";
		}

		$objResponse->script( '$(\'#navi_trash_'.$aFormValues['project_id'].' .icon\').css(\'background-image\',\'url('.$icon.')\');' );
		$objResponse->script( '$(\'#navi_trash_'.$aFormValues['project_id'].' .name\').text(\'Papierkorb ('.$row['anzahl'].')\');' );

		return $objResponse;
	}


	function TrashDeleteAll ($aFormValues) {
		$objResponse = new ccdsResponse();


		$deleted_objects = array ();

		$query = "SELECT obj_id FROM obj_index
	           WHERE 1
	           AND obj_deleted='1'
	           AND obj_key_project_id='".$aFormValues['project_id']."'
	
	           ";
		$res = $this->ccdSQL->query( $query );

		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$this->tools->ObjectDelete( $row['obj_id'] );

		}
		$objResponse->loadcommands( $this->TrashReload( $aFormValues ) );
		$objResponse->loadcommands($this->TrashListing( $aFormValues ) );
		return $objResponse;
	}
}


